<?php
class Model_UserTests extends Model_Base
{
	public function isValid($email, $testId)
	{
		return true;
	}
	
	public function save($testId)
	{
		HackerU_Session::getInstance()->testId = $testId;
	}
	
	public function getTestDetails($testId)
	{
		$table = new Model_DbTable_Tests();
		$testDetailsFromDb = $table->getTestDetails($testId);
		$result = array();
		foreach($testDetailsFromDb as $row){
			
			//create the test array
			if(!isset($result[$row->testId])){
				$result[$row->testId] = array();
				$result[$row->testId]['title'] = $row->name.' -- '.$row->subject;
			}
			
			
			//create array per question
			if(!isset($result[$row->testId][$row->qId])){
				$result[$row->testId][$row->qId] = array();
				$result[$row->testId][$row->qId]['title'] = $row->q;
			}
			
			//add the answers to the question array
			$result[$row->testId][$row->qId][ ] = array(
				'ansId' => $row->ansId,
				'title' => $row->ans
			);
		}
		return $result;
	}
	
	public function getResult($testId, array $answerIds)
	{
		$questionTable = new Model_DbTable_Questions();
		$numOfQuestions = $questionTable->getNumOfQuestions($testId);
		
		
		$answerTable = new Model_DbTable_Answers();
		$correctAnswers = $answerTable->getNumOfCorrectAnswers($answerIds);
		return number_format((100/$numOfQuestions)*$correctAnswers);
	}
}